﻿using NfsCommon.Util;
using NfsModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
namespace NfsBLL
{
    public partial class SalesManBLL
    {
        public SalesManBLL(){}
        private readonly NfsDAL.SalesManDAL dal = new NfsDAL.SalesManDAL();
        
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public salesman GetModel(int id)
        {

            return dal.GetModel(id);
        }
        public DataTable GetMaxCode()
        {

            return dal.GetMaxCode();
        }
        public List<salesman> GetSalesManBycondtion(int pageindex, int pagesize)
        {
            DataTable dt = dal.GetSalesManBycondtion(pageindex, pagesize);
            return ConverToExcs.ConvertToEx<salesman>(dt);
        }
        public object GetSalesManBycondtionCount()
        {
            return dal.GetSalesManBycondtionCount();
        }
        public bool GetSingle(int id, string name, string code)
        {
            return dal.GetSingle(id, name, code);
        }
        #region 
        public bool InsertSalesMan(salesman model, string code)
        {

            bool flag = true;
          
            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                  
                    flag = this.Add(model);
                    if (flag)
                    {
                        
                    }
                    if (flag)
                    {
                        trans.Complete();
                    }
                }
                catch (Exception ex)
                {
                    flag = false;
                }
            }

            return flag;
        }
        #endregion
        #region 
        public bool updateSalesMan(salesman model, string code)
        {

            bool flag = true;
            int agId = 0;

            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                     
                        flag = this.Update(model);
                    
                    if (flag)
                    {
                        trans.Complete();
                    }
                }
                catch (Exception ex)
                {
                    flag = false;
                }
            }

            return flag;
        }
        #endregion

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(NfsModel.salesman model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(NfsModel.salesman model)
        {
            return dal.Update(model);
        }
        public List<salesman> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        public List<salesman> DataTableToList(DataTable dt)
        {
            List<salesman> modelList = new List<salesman>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                salesman model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }
    }
}
